home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtozin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  3.1 KB  |  130 lines

  1. /* pbmtozinc.c - read a portable bitmap and produce an bitmap file
  2. **               in the format used by the Zinc Interface Library (v1.0)
  3. **               November 1990.
  4. **
  5. ** Author: James Darrell McCauley
  6. **         Department of Agricultural Engineering
  7. **         Texas A&M University
  8. **         College Station, Texas 77843-2117 USA
  9. **
  10. ** Copyright (C) 1988 by James Darrell McCauley (jdm5548@diamond.tamu.edu)
  11. **                    and Jef Poskanzer.
  12. **
  13. ** Permission to use, copy, modify, and distribute this software and its
  14. ** documentation for any purpose and without fee is hereby granted, provided
  15. ** that the above copyright notice appear in all copies and that both that
  16. ** copyright notice and this permission notice appear in supporting
  17. ** documentation.  This software is provided "as is" without express or
  18. ** implied warranty.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include "pbm.h"
  23.  
  24. void
  25. main( argc, argv )
  26.     int argc;
  27.     char* argv[];
  28.     {
  29.     FILE* ifp;
  30.     bit* bitrow;
  31.     register bit* bP;
  32.     int rows, cols, format, padright, row;
  33.     register int col;
  34.     char name[100];
  35.     char* cp;
  36.     int itemsperline;
  37.     register int bitsperitem;
  38.     register int item;
  39.     int firstitem;
  40.     char* hexchar = "084c2a6e195d3b7f";
  41.  
  42.     pbm_init( &argc, argv );
  43.  
  44.     if ( argc > 2 )
  45.     pm_usage( "[pbmfile]" );
  46.  
  47.     if ( argc == 2 )
  48.     {
  49.     ifp = pm_openr( argv[1] );
  50.     strcpy( name, argv[1] );
  51.     if ( strcmp( name, "-" ) == 0 )
  52.         strcpy( name, "noname" );
  53.  
  54.     if ( ( cp = index( name, '.' ) ) != 0 )
  55.         *cp = '\0';
  56.     }
  57.     else
  58.     {
  59.     ifp = stdin;
  60.     strcpy( name, "noname" );
  61.     }
  62.  
  63.     pbm_readpbminit( ifp, &cols, &rows, &format );
  64.     bitrow = pbm_allocrow( cols );
  65.  
  66.     /* Compute padding to round cols up to the nearest multiple of 16. */
  67.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  68.  
  69.     fprintf (stdout,  "USHORT %s[] = {\n",name);
  70.     fprintf (stdout,  "  %d\n", cols );
  71.     fprintf (stdout,  "  %d\n", rows );
  72.  
  73.     itemsperline = 0;
  74.     bitsperitem = 0;
  75.     item = 0;
  76.     firstitem = 1;
  77.  
  78. #define PUTITEM \
  79.     { \
  80.     if ( firstitem ) \
  81.     firstitem = 0; \
  82.     else \
  83.     putchar( ',' ); \
  84.     if ( itemsperline == 11 ) \
  85.     { \
  86.     putchar( '\n' ); \
  87.     itemsperline = 0; \
  88.     } \
  89.     if ( itemsperline == 0 ) \
  90.     putchar( ' ' ); \
  91.     ++itemsperline; \
  92.     putchar('0'); \
  93.     putchar('x'); \
  94.     putchar(hexchar[item & 15]); \
  95.     putchar(hexchar[(item >> 4) & 15]); \
  96.     putchar(hexchar[(item >> 8) & 15]); \
  97.     putchar(hexchar[item >> 12]); \
  98.     bitsperitem = 0; \
  99.     item = 0; \
  100.     }
  101.  
  102. #define PUTBIT(b) \
  103.     { \
  104.     if ( bitsperitem == 16 ) \
  105.     PUTITEM; \
  106.     if ( (b) == PBM_BLACK ) \
  107.     item += 1 << bitsperitem; \
  108.     ++bitsperitem; \
  109.     }
  110.  
  111.     for ( row = 0; row < rows; ++row )
  112.     {
  113.     pbm_readpbmrow( ifp, bitrow, cols, format );
  114.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  115.         PUTBIT( *bP );
  116.     for ( col = 0; col < padright; ++col )
  117.         PUTBIT( 0 );
  118.         }
  119.  
  120.     pm_close( ifp );
  121.     
  122.     if ( bitsperitem > 0 )
  123.     PUTITEM;
  124.     fprintf (stdout,  "};\n" );
  125.  
  126.     pm_close (stdout);
  127.  
  128.     exit( 0 );
  129.     }
  130.